<template>
  <a-modal
    :title='title'
    :width='800'
    :visible='visible'
    :okButtonProps='{ props: {disabled: disableSubmit} }'
    :confirmLoading='confirmLoading'
    @ok='handleOk'
    @cancel='handleCancel'
    cancelText='关闭'>
    <a-spin :spinning='confirmLoading'>
      <a-form :form='form'>
        <a-form-item
          :labelCol='labelCol'
          :wrapperCol='wrapperCol'
          label='商品id'
          :hidden='hiding'>
          <a-input placeholder='请输入商品id' v-decorator="['productId', {'initialValue':this.mainId}]" />
        </a-form-item>

        <a-form-item
          :labelCol='labelCol'
          :wrapperCol='wrapperCol'
          label='经销商'
          :hidden='editEnable'>
          <a-input v-decorator="['storeId_dictText', {} ]" :disabled='true' />
        </a-form-item>

        <a-form-item
          :labelCol='labelCol'
          :wrapperCol='wrapperCol'
          label='经销商'
          :hidden='!editEnable'>
          <j-multi-select-tag v-decorator="['storeIds', {}]" placeholder='请选择经销商'
                              dictCode='stm_store_info,name,store_id,status!=0 and store_type=1 and brand_category=2' />
        </a-form-item>

        <a-row class='form-row' :gutter='24'>
          <a-col :span='10' :offset='4'>
            <a-form-item
              :labelCol='labelCol'
              :wrapperCol='wrapperCol'
              label='价格'>
              <a-input-number v-decorator="[ 'price', validatorRules.price]" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import { getAction } from '@/api/manage'
import pick from 'lodash.pick'
import moment from 'moment'
import JDate from '@/components/jeecg/JDate'
import AFormItem from 'ant-design-vue/es/form/FormItem'
import JMultiSelectTag from '@/components/dict/JMultiSelectTag'

export default {
  components: {
    AFormItem,
    JDate,
    JMultiSelectTag
  },
  name: 'PmsSkuStockModalJXV2',
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 14 }
      },
      moment,
      format: 'YYYY-MM-DD HH:mm:ss',
      disableSubmit: false,
      mainId: '',
      hiding: false,
      editEnable: true,
      confirmLoading: false,
      form: this.$form.createForm(this),
      validatorRules: {
        skuCode: { rules: [{ required: true, message: '请输入物料编码!' }] },
        sapItemCode: { rules: [{ required: true, message: '请输入物料编号!' }] },
        price: { rules: [{ required: true, message: '请填写价格!' }] },
        promotionPrice: { rules: [{ required: true, message: '请填写促销价格!' }] },
        currentStock: { rules: [{ required: true, message: '请填写可用库存!' }] }
      },
      url: {
        add: '/pms/pmsProduct/addPmsSkuStock',
        edit: '/pms/pmsProduct/editPmsSkuStock',
        queryAttr: '/pms/pmsProduct/queryAttrById'
      }
    }
  },
  created() {
  },
  methods: {

    add(mainId) {
      if (mainId) {
        this.edit({ mainId }, '')
      } else {
        this.$message.warning('请选择一条数据')
      }
    },
    detail(record) {
      this.edit(record, 'd')
    },
    edit(record, v) {
      if (v == 'e') {
        this.hiding = false
        this.disableSubmit = false
      } else if (v == 'd') {
        this.hiding = false
        this.disableSubmit = true
      } else {
        this.hiding = true
        this.disableSubmit = false
      }
      this.editEnable = true
      this.mainId = record.mainId
      if (this.mainId === undefined || this.mainId === null || this.mainId === '') {
        this.editEnable = false
        this.mainId = record.productId
      }
      this.form.resetFields()
      console.log(this.mainId)
      this.model = Object.assign({}, record)

      if (this.model.stock === undefined || this.model.stock === null || this.model.stock === '') {
        this.model.stock = 0
        this.model.currentStock = 0
      } else {
        if (this.model.lockStock === undefined || this.model.lockStock === null || this.model.lockStock === '') {
          this.model.lockStock = 0
        }
        this.model.currentStock = this.model.stock - this.model.lockStock
      }

      if (this.model.skuSpec === undefined || this.model.skuSpec === null || this.model.skuSpec === '') {
        this.model.skuSpec = 1
      }

      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, 'storeId_dictText', 'productId', 'skuCode', 'sapItemCode', 'price', 'stock', 'lowStock', 'currentStock', 'pic', 'promotionPrice', 'skuUnit', 'skuSpec'))
        // 时间格式化
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          formData.promotionPrice = formData.price
          //时间格式化
          httpAction(httpurl, formData, method).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.$emit('ok')
              that.close()
            } else {
              that.$message.warning(res.message)
            }
          }).finally(() => {
            that.confirmLoading = false
          })
        }
      })
    },
    handleCancel() {
      this.close()
    }
  }
}
</script>

<style scoped>

</style>
